package com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu.rabbittrunk;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

import com.esun.util.log.EDebug;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;


/** 
* @ClassName: LotteryUtils 
* @Description: TODO(彩票工具类) 
* @author Miko baoz@500wan.com
* @date 2011-1-13 下午09:27:54 
*  
*/
public class LotteryUtils {
	
//    private static Context context;
	
	/***
	 * 将set内的元素添加到String[]中
	 * @param set 需要转换的set集
	 * @author Miko
	 */
	public static String[] setToStringArray(Set<Integer> set) {

		Object[] obj = set.toArray();

		String[] strTemp = new String[obj.length];
		//
		// set.
		for (int i = 0; i < obj.length; i++) {
			strTemp[i] = String.valueOf(obj[i]);
		}

		return strTemp;
	}
	
	
	/***
	 * 选出不重复随机号码
	 * @param i 随机号码个数
	 * @param max 随机号码最大值
	 */
	public static StringBuilder randomNum(int howMany,int max){

	    EDebug.o(LotteryUtils.class,"Doing random auto howMany [" + String.valueOf(howMany) + "] max [" + String.valueOf(max) + "]");
	    
		Random random = new Random();

		HashSet<Integer> randomArray = new HashSet<Integer>();
		
		StringBuilder sb = new StringBuilder();
		

		while (true) {
			int temp = random.nextInt(max + 1);
			if(temp!=0){
				randomArray.add(temp);
			}
			if(randomArray.size() == howMany){
				break;
			}
		}
		
		TreeSet<Integer> tree = new TreeSet<Integer>();
		tree.addAll(randomArray);
		
		Iterator<Integer> iterator = tree.iterator();
		
		while(iterator.hasNext()){
			if (sb.length() == 0) {
				sb.append(iterator.next());
			} else {
				sb.append(",");
				sb.append(iterator.next());
			}
		}
		
		return sb;
	}
	
	
	
	
	/***
	 * 将复试选号拆成单注组合
	 * @param head 前部分号码个数
	 * @param foot 后部分号码个数
	 * @param arrayHead 前部分号码
	 * @param arrayFoot 后部分号码
	 * 
	 * @return 发送给Server的号码列表
	 * 
	 * @author Miko
	 */
	public static String[] voteSeparate(int head ,int foot ,Set<Integer> setHead, Set<Integer> setFoot){
		
		TreeSet<Integer> treeHead = new TreeSet<Integer>();
		TreeSet<Integer> treeFoot = new TreeSet<Integer>();
		treeHead.addAll(setHead);
		treeFoot.addAll(setFoot);
		
		String[] first = setToStringArray(treeHead);
		String[] last  = setToStringArray(treeFoot);
		ArrayList<String> al = new ArrayList<String>();
		
		
		first = Combination.getCombination(first, head);
		last  = Combination.getCombination(last, foot);
		
		for (int i = 0; i < last.length; i++) {
			
			for (int j = 0; j < first.length; j++) {
				
				StringBuilder sb = new StringBuilder();
				
				sb.append(first[j]);
				sb.append("|");
				sb.append(last[i]);
				
				al.add(sb.toString());
			}
		}
		
		String[] strReturn = new String[al.size()];
		for(int i=0;i<al.size();i++){
			strReturn[i] = al.get(i);
			EDebug.o(strReturn[i]);
		}
		return strReturn;
	}
	
	
	/***
	 * 小于10的数字前面添上0
	 * @param sb 需要转换的字符串
	 * @return sbReturn 转换后的StringBuilder
	 * 
	 * @author Miko
	 */
	public static StringBuilder sbAddZero(StringBuilder sb){
		
		String[] temp = sb.toString().split(",");
		
		StringBuilder sbReturn = new StringBuilder();
		
		for(int i=0;i<temp.length;i++){
			if(temp[i].length()<2){
				sbReturn.append(0).append(temp[i]).append(",");
			}else{
				sbReturn.append(temp[i]).append(",");
			}
		}
		
		sbReturn.setLength(sbReturn.length() - 1);
		
		return sbReturn;
	}
	
	
	
	/***
	 * 将每一注的彩票号码按升序排列
	 * @param str 需要转换的字符串
	 * @return str 转换后的String[]
	 * 
	 * @author Miko
	 */
	public static String[] AscendingOrder(String[] str){
		
		for(int i=0;i<str.length;i++){
			
			
			String[] temp = str[i].replace("|", "@").split("@");
			
			String[] head = temp[0].split(",");
			String[] foot = temp[1].split(",");
			
			TreeSet<String> headSet = new TreeSet<String>();
			TreeSet<String> footSet = new TreeSet<String>();
			
			for(int j=0;j<head.length;j++){
				headSet.add(head[j]);
			}
			Object[] obj;
			obj = headSet.toArray();
			for(int j=0;j<head.length;j++){
				head[j] = obj[j].toString();
			}
			
			for(int j=0;j<foot.length;j++){
				footSet.add(foot[j]);
				
			}
			obj = footSet.toArray();
			for(int j=0;j<foot.length;j++){
				foot[j] = obj[j].toString();
			}
			
			StringBuilder sb = new StringBuilder();
			
			for(int j=0;j<head.length;j++){
				sb.append(head[j]).append(",");
			}
			sb.setLength(sb.length()-1);
			sb.append("|");
			for(int j=0;j<foot.length;j++){
				sb.append(foot[j]).append(",");
			}
			sb.setLength(sb.length()-1);
			str[i] = sb.toString();
		}
		
		return str;
	}
	
	
	/***
	 * 将所有的checkbox控件置为false状态
	 * @param ht checkbox的hashtable 集
	 * 
	 * @author Miko
	 */
	public static void resetTable(Hashtable<Integer, CheckBox> ht){
		Iterator<CheckBox> iterator = ht.values().iterator();
		while(iterator.hasNext()){
			iterator.next().setChecked(false);
		}
	}
	
	/***
     * 胆拖投注准备方法
     * @param sbDare 胆码
     * @param sbDelay 胆码
     * @author Miko
     */
	public static String dareAndDelayVoteSeparate(StringBuilder sbDare, StringBuilder sbDelay){
	    
	    StringBuilder sbTemp = new StringBuilder();
	    sbDare = sbAddZero(sbDare);
	    sbDelay = sbAddZero(sbDelay);
	    if(sbDare.toString().equals(DataShare.NUM0 + DataShare.NUM0)){
	        sbDare.setLength(0);
	    }
	    sbTemp.append("[D:").append(sbDare.toString()).append("][T:").append(sbDelay.toString()).append("]");
	    
	    return sbTemp.toString();
	}
	
	/***
     * 投注准备
     * @param sbDare 前部分
     * @param sbDelay 后部分
     * @author Miko
     */
	public static String voteBiuld(String head , String foot){
	    
	    return head + "|" + foot;
	}
	
	
	
	
	public static ProgressDialog buildAProgress(Context context,String msg){
	    
	    ProgressDialog progress = null;
	    progress = new ProgressDialog(context);
//	    progress.setTitle("请稍后");
	    progress.setMessage(msg);
	    
	    progress.setOnKeyListener(new DialogInterface.OnKeyListener() {
            
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                
                return true;
                
            }
        });
	    
	    return progress;
	}
	
	
	
	
	
	 
	
	//组合公式    C(n,m) = n!/(m!(n-m)!) 从N个数里选M个
	public static int getVoteline(int n, int m){
	    
	    int f=1;
        for(int i = 1;i <= m;i++)
            f = f*(i+(n-m))/i;
        
        return f;

    }

    /**
     * @Title: pushCheckBoxToArray
     * @Description: TODO(将监听器加入每个checkbox)
     * @param id
     *            checkBox ID[]
     * @param parentView
     *            承载选球键盘的FatherView
     * @param listener
     *            监听器
     * @param table 放入checkBox的table
     * @return void 返回类型
     */
    public static void pushCheckBoxToArray(int[] id, View parentView,
            CompoundButton.OnCheckedChangeListener listener,
            Hashtable<Integer, CheckBox> table) {

        for (int i = 0; i < id.length; i++) {
            CheckBox cb = (CheckBox) parentView.findViewById(id[i]);
            cb.setChecked(false);
            cb.setOnCheckedChangeListener(listener);
            table.put(Integer.parseInt(String.valueOf(cb.getTag())), cb);
        }
    }
    
    /** 
    * @Title: getLotRest 
    * @Description: TODO(用于在投注时，显示购买成功后剩余金额) 
    * @param userRest 用户余额
    * @param buying 购买金额
    * @return String 购买剩余金额
    */
    public static String getLotRest(String userRest, int buying){
        
        double userRestTemp;
//        float buyingTemp;
        
        userRestTemp = Double.parseDouble(userRest);
        
        EDebug.o(LotteryUtils.class,"input UserRest is : " + userRest);
        EDebug.o(LotteryUtils.class,"input buying is : " + buying);
        
//        buyingTemp = Float.parseFloat(String.valueOf(buying));
        //String afterBet=String.valueOf(userRestTemp - buying);
        double doubelTemp = userRestTemp - buying;
        EDebug.o(LotteryUtils.class,"input userRestTemp - buying is : " + String.valueOf(doubelTemp));
//        System.out.println("input userRestTemp - buying is : " + String.valueOf(doubelTemp));
        DecimalFormat   format   =   new   DecimalFormat( "0.00"); 
        format.setMaximumFractionDigits(3); 
        String afterBet=format.format(doubelTemp);
//            new DecimalFormat("##########.##").format(Double.toHexString(doubelTemp));
//            String.valueOf(doubelTemp);
        if(doubelTemp >= 0){
            return afterBet + "元";
        }else{
            return "不足以完成投注";
        }
    }

}
